package com.xumt.hwHMSdemo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.xumt.hwHMSdemo.cannectSb.HttpRequest;

import org.json.JSONArray;
import org.json.JSONObject;

public class individualms extends AppCompatActivity {
    private String result;
    private String thisusername=Login2.usernamen;
    private String sex=null;
    public static String nicheng=Login2.usernamen;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_individualms);
        Intent intent=new Intent(this,NewmainActivity.class);
        Log.i("username",thisusername);
        findms();

                try {
                    JSONArray jsonArray = new JSONArray(result);
                    String name = null;
                    String phonenumber = null;
                    String Email = null;
                    String sex = null;
                    String address = null;
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObject = jsonArray.getJSONObject(i);
                        name = jsonObject.getString("username");
                        phonenumber = jsonObject.getString("phonenumber");
                        Email = jsonObject.getString("email");
                        sex = jsonObject.getString("sex");
                        address = jsonObject.getString("address");
                        nicheng=name;

                    }
                    Log.i("lallaa", "111");
                    TextView textView = findViewById(R.id.editTextTextPersonName);
                    textView.setText(name);

                    TextView textView1 = findViewById(R.id.phonenumber);
                    textView1.setText(phonenumber);

                    TextView textView3 = findViewById(R.id.Email);
                    textView3.setText(Email);


                    RadioButton radioButton = (RadioButton) findViewById(R.id.man);
                    RadioButton radioButton1 = (RadioButton) findViewById(R.id.women);
                    if (sex.equals("man")) {
                        radioButton.setChecked(true);
                    } else {
                        radioButton1.setChecked(true);
                    }


                } catch (Exception e) {
                    e.printStackTrace();
                }




        Button button1=(Button)findViewById(R.id.submit);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TextView textView = findViewById(R.id.editTextTextPersonName);
                TextView textView1 = findViewById(R.id.phonenumber);
                TextView textView3 = findViewById(R.id.Email);
                RadioButton radioButton = (RadioButton) findViewById(R.id.man);
                RadioButton radioButton1 = (RadioButton) findViewById(R.id.women);
                String name=textView.getText().toString();
                String phone=textView1.getText().toString();
                String Email=textView3.getText().toString();
                if (radioButton.isChecked())
                {
                    sex="man";
                }
                else {
                    sex="women";
                }


                HttpRequest request = new HttpRequest();
                new Thread() {

                    public void run() {
                        try {

                            result = request.sendGet("http://169.254.67.221:8081/user/insert", "name=" + thisusername +"&"+"username="+name+"&"+
                                    "phonenumber="+phone+"&"+"Email="+Email+"&"+"sex="+sex);

                            //   System.out.println(request.sendGet("http://169.254.67.221:8080/user/login", "username=wang&password=123"));

                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }.start();

                Toast.makeText(individualms.this, "SET SUCCESS", Toast.LENGTH_SHORT).show();
            }
        });

        Button button2=(Button)findViewById(R.id.exit);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               startActivity(intent);
            }
        });

    }

    public void findms()
    {
        HttpRequest request = new HttpRequest();
        new Thread() {

            public void run() {
                try {

                    result = request.sendGet("http://169.254.67.221:8081/user/findms", "name=" + thisusername);
                    //   System.out.println(request.sendGet("http://169.254.67.221:8080/user/login", "username=wang&password=123"));
                   Log.i("resule",result);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}